package com.mydesignpattern.beijing.future.jdkfuture;

import com.mydesignpattern.beijing.future.future.Student;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class JdkFutureTest implements Callable<List<Student>>{

    private String cond = null;
    public JdkFutureTest(String cond){
        this.cond = cond;
    }

    @Override
    public List<Student> call() throws Exception {
        System.out.println("根据条件："+cond+"查询列表");
        Thread.sleep(5000);
        //组装数据
        List<Student> list = new ArrayList<>();
        list.add(new Student("1","jack1","21","boy"));
        list.add(new Student("2","jack2","22","boy"));
        list.add(new Student("3","jack3","23","boy"));
        list.add(new Student("4","jack4","24","boy"));
        return list;
    }

    public static void main(String[] args) {
        FutureTask ft = new FutureTask(new JdkFutureTest("age >25"));
        ExecutorService es = Executors.newCachedThreadPool();
        es.submit(ft);

        System.out.println("子线程已经开始运行");
        try {
            System.out.println("主线程运行其它相关业务");
            Thread.sleep(2000);
            System.out.println("获取到的数据是："+ft.get());
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        es.shutdown();
    }
}
